package com.swust.reactredis.normal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

@Component
public class NormalUsage<T> {

    private ReactiveRedisTemplate<String, T> reactiveRedisTemplate;

    @Autowired
    public void setRedisTemplate(ReactiveRedisTemplate reactiveRedisTemplate) {
        this.reactiveRedisTemplate = reactiveRedisTemplate;
    }

    public Mono<Boolean> setValue(String key, T value, long expireTime, ChronoUnit chronoUnit) {
        return reactiveRedisTemplate.opsForValue().set(key, value, Duration.of(expireTime, chronoUnit));
    }

    public Mono<T> getValue(String key) {
        //根据key查询value
        return reactiveRedisTemplate.opsForValue().get(key);
    }

    /**
     * 获取节点信息
     * {
     * 	"aof_rewrite_scheduled": "0",
     * 	"keyspace_misses": "50",
     * 	"mem_allocator": "jemalloc-3.6.0",
     * 	"multiplexing_api": "WinSock_IOCP",
     * 	"aof_last_write_status": "ok",
     * 	"run_id": "5dbe5c0c18772ad8c1043132078e26aeb03dce7c",
     * 	"uptime_in_seconds": "510654",
     * 	"arch_bits": "64",
     * 	"master_repl_offset": "0",
     * 	"instantaneous_output_kbps": "0.00",
     * 	"total_commands_processed": "23821",
     * 	"repl_backlog_size": "1048576",
     * 	"sync_full": "0",
     * 	"sync_partial_err": "0",
     * 	"used_cpu_user": "1.70",
     * 	"aof_enabled": "0",
     * 	"redis_mode": "standalone",
     * 	"rdb_changes_since_last_save": "111",
     * 	"used_memory_lua": "36864",
     * 	"redis_build_id": "a4f7a6e86f2d60b3",
     * 	"connected_slaves": "0",
     * 	"expired_keys": "17",
     * 	"used_memory": "1050584",
     * 	"used_memory_rss": "991936",
     * 	"rdb_last_bgsave_status": "ok",
     * 	"process_id": "6632",
     * 	"rejected_connections": "0",
     * 	"rdb_bgsave_in_progress": "0",
     * 	"cluster_enabled": "0",
     * 	"tcp_port": "6379",
     * 	"os": "Windows  ",
     * 	"aof_current_rewrite_time_sec": "-1",
     * 	"repl_backlog_histlen": "0",
     * 	"role": "master",
     * 	"used_cpu_sys": "1.39",
     * 	"aof_last_bgrewrite_status": "ok",
     * 	"used_memory_peak_human": "1.79M",
     * 	"hz": "10",
     * 	"used_memory_human": "1.00M",
     * 	"mem_fragmentation_ratio": "0.94",
     * 	"rdb_last_bgsave_time_sec": "0",
     * 	"blocked_clients": "0",
     * 	"used_cpu_sys_children": "0.00",
     * 	"repl_backlog_active": "0",
     * 	"used_cpu_user_children": "0.00",
     * 	"sync_partial_ok": "0",
     * 	"aof_rewrite_in_progress": "0",
     * 	"redis_git_sha1": "00000000",
     * 	"client_longest_output_list": "0",
     * 	"rdb_current_bgsave_time_sec": "-1",
     * 	"aof_last_rewrite_time_sec": "-1",
     * 	"config_file": "C:Program FilesRedis\redis.windows-service.conf",
     * 	"loading": "0",
     * 	"pubsub_channels": "1",
     * 	"db0": "keys=3,expires=0,avg_ttl=0",
     * 	"migrate_cached_sockets": "0",
     * 	"redis_git_dirty": "0",
     * 	"connected_clients": "18",
     * 	"lru_clock": "3516743",
     * 	"pubsub_patterns": "12",
     * 	"repl_backlog_first_byte_offset": "0",
     * 	"total_net_output_bytes": "4580773",
     * 	"client_biggest_input_buf": "0",
     * 	"evicted_keys": "0",
     * 	"total_connections_received": "390",
     * 	"redis_version": "3.0.504",
     * 	"total_net_input_bytes": "4868555",
     * 	"latest_fork_usec": "3212",
     * 	"used_memory_peak": "1881176",
     * 	"keyspace_hits": "6984",
     * 	"uptime_in_days": "5",
     * 	"instantaneous_input_kbps": "0.00",
     * 	"rdb_last_save_time": "1714792591",
     * 	"instantaneous_ops_per_sec": "0"
     * }
     * @return  节点信息，可判断是否为主节点，key为role
     */
    public Mono<String> getNodeInfo() {
        return reactiveRedisTemplate.getConnectionFactory().getReactiveConnection().serverCommands().info().map(info -> {
            // 对info字符串进行解析，找到主节点相关的信息
            // 这里只是一个简单的示例，实际解析应该根据info命令的输出格式进行
            return "主节点信息: " + info;
        });
    }
}